import type { DirectiveBinding } from 'vue';

// 这会在 `mounted` 和 `updated` 时都调用
const hasRoles = (el: any, binding: DirectiveBinding<string[]>) => {
  const { value } = binding;
  if (!value || !value.length) throw new Error('v-has-roles 指令的值不能为空');
  if (!el.parentNode) throw new Error('v-has-roles 不能用于根节点！');

  const { roles } = useUserStore();
  const hasRole = roles.some((role) => hasAdminRole(role) || value.includes(role));

  // 没有角色则移除节点
  if (!hasRole) {
    el.parentNode.removeChild(el);
  }
};

export default hasRoles;
